import os
import yaml
from utils.data_path_manager import data_path_manager


class DataLoader:
    @classmethod
    def load_case_data(cls, test_type, directory, filename):
        # 判断测试类型，返回测试数据的path
        if test_type == "web":
            pre_path = f"{data_path_manager.page_data_dir}"
        elif test_type == "api":
            pre_path = f"{data_path_manager.api_data_dir}"
        else:
            pre_path = f"{data_path_manager.app_data_dir}"
        path = os.path.join(pre_path, directory, filename)
        with open(path, "r", encoding="utf-8") as f:
            data = yaml.safe_load(f)
        if data:
            return data
        else:
            raise Exception("Failed load test case data!")

    @classmethod
    def write_date(cls, data, path=f"{data_path_manager.project_dir}/data/token.yml"):
        data = {
            "access_token": data
        }
        with open(path, "a+", encoding="utf-8") as f:
            yaml.dump(data, f, allow_unicode=True)

# if __name__ == '__main__':
#     # utils = Utils()
#     # utils.clear_all_calendar()
#     # test_data_path = os.path.join(f"{data_path_manager.api_data_dir}", "calendar", "calendar.yml")
#     # data = DataLoader.load_test_case_data(test_data_path)['create_calendar']['data']
#     create_calendar = DataLoader.load_case_data('calendar', 'calendar.yml')['create_calendar']
#     search_calendar = DataLoader.load_case_data('calendar', 'calendar.yml')['search_calendar']
#     ids = [d.pop(0) for d in create_calendar]
#     print(search_calendar)
#     # print(ids)